package com.usian.controller;

import com.usian.pojo.User;
import com.usian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("findAll")
    public String findAll(Model model){
        List<User> list = userService.findAll();
        model.addAttribute("list",list);
        return "list";
    }

    @RequestMapping("findById")
    public String findById(Model model,Integer id){
        User byId = userService.findById(id);
        model.addAttribute("user",byId);
        return "edit";
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        userService.delete(id);
        return "redirect:/findAll";
    }

    @RequestMapping("add")
    public String add(User user){
        userService.add(user);
        return "redirect:/findAll";
    }

    @RequestMapping("edit")
    public String  edit(User user){
        userService.edit(user);
        return "redirect:/findAll";
    }

    @RequestMapping("goAdd")
    public String goAdd(){
        return "add";
    }
}
